package com.jiazhong.equals;

import java.util.Comparator;
import java.util.Date;

public class User implements Comparable<User>{
    private Integer userID;
    private String userName;
    private Integer userScore;
    private Date regDate;

    @Override
    public int compareTo(User o) {
        return this.getUserID()-o.getUserID();
    }


    @Override
    public boolean equals(Object obj) {
        System.out.println("执行user的equals方法");
        if (obj==null){
            return false;
        }
        if (obj instanceof User){
            User user=(User)obj;
            return this.userID==user.getUserID()&&this.userName.equals(user.getUserName());
        }
        return false;
    }

    @Override
    public int hashCode() {
        System.out.println("执行user的hashCode方法");
        return 1;
    }

    public User() {
    }

    public User(Integer userID, String userName) {
        this.userID = userID;
        this.userName = userName;
    }

    public User(Integer userID, String userName, Integer userScore, Date regDate) {
        this.userID = userID;
        this.userName = userName;
        this.userScore = userScore;
        this.regDate = regDate;
    }

    public Integer getUserScore() {
        return userScore;
    }

    public void setUserScore(Integer userScore) {
        this.userScore = userScore;
    }

    public Date getRegDate() {
        return regDate;
    }

    public void setRegDate(Date regDate) {
        this.regDate = regDate;
    }

    public Integer getUserID() {
        return userID;
    }

    public void setUserID(Integer userID) {
        this.userID = userID;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "User{" +
                "userID=" + userID +
                ", userName='" + userName + '\'' +
                ", userScore=" + userScore +
                ", regDate=" + regDate +
                '}';
    }
    /**
     * 使用内部类定义扩展规则
     * 1.根据积分升序排序
     */
    static class sortByScoreASC implements Comparator<User>{

        @Override
        public int compare(User o1, User o2) {
            return o1.getUserScore()- o2.getUserScore();
        }
    }

    /**
     * 2.根据积分降序排序
     */
    static class sortByScoreDESC implements Comparator<User>{
        @Override
        public int compare(User o1, User o2) {
            return o2.getUserScore()-o1.getUserScore();
        }
    }

    /**
     * 3.根据学号降序排序
     */
    static class sortByUserIdEsc implements Comparator<User>{
        @Override
        public int compare(User o1, User o2) {
            return o1.getUserID()-o2.getUserID();
        }
    }
}
